package cn.marinda;

import cn.marinda.entity.Server;
import cn.marinda.entity.ServerInfo;
import cn.marinda.entity.ServerProcess;
import cn.marinda.gui.impl.*;

import java.io.File;
import java.util.concurrent.*;

import cn.marinda.runnable.*;
import cn.marinda.util.FileUtils;

import javax.swing.*;

/**
 * minecraft服务器工具
 *
 * @author Marinda
 * @date 2022/07/06
 */
public class MinecraftServerHelper {

    public static ExecutorService executorService = Executors.newSingleThreadExecutor();
    public static ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(50);
    public static ServerInfo serverInfo;
    public static ServerProcess process = ServerProcess.instance;
    public static String version = "1.1";
    public static void main(String[] args) {

        mcServerHelper();
    }

    /**
     * mc服务器辅助启动入口方法
     */
    public static void mcServerHelper() {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
//            ConfigManager.configManager.onLoadAllConfiguration();
            registerRunnable();

            ApplicationHelperImpl.instance.open();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private static void registerRunnable(){
        serverInfo = new ServerInfo();
        registerInfoRunnable();
        registerServerRunnable();
//        registerDownloadRunnable();
    }


    /**
     * 注册信息可运行
     */
    private static void registerInfoRunnable(){
        ServerInfoRunnable serverInfoRunnable = new ServerInfoRunnable(serverInfo,ServerConsoleHelperImpl.instance.frame);
        TimeRunnable timeRunnable = new TimeRunnable(ApplicationHelperImpl.instance.frame);
        scheduledExecutorService.scheduleWithFixedDelay(timeRunnable,0,1,TimeUnit.SECONDS);
        scheduledExecutorService.scheduleWithFixedDelay(serverInfoRunnable,0,1, TimeUnit.SECONDS);
    }

    /**
     * 注册服务器可运行
     */
    private static void registerServerRunnable(){
        ServerConsoleRunnable consoleRunnable = new ServerConsoleRunnable(process,(JTextArea) ServerConsoleHelperImpl.instance.getHelperComponent("TextAreaUI"));
        scheduledExecutorService.scheduleWithFixedDelay(consoleRunnable,0,1, TimeUnit.SECONDS);
    }

}
